import time
from pyecharts.charts import Line
import akshare as ak
import pyecharts.options as opts
from pyecharts.globals import ThemeType
from model.stock_index import StockIndex

# 指数走势对比
startDate = '20160101'
endDate = time.strftime('%Y%m%d', time.localtime(time.time()))

symbolList = [
    # StockIndex('000001', '上证指数'),
    # StockIndex('000990', '全指消费'),
    # StockIndex('000989', '全指可选'),
    # StockIndex('000991', '全指医药'),
    # StockIndex('000992', '全指金融地产'),
    # StockIndex('000993', '全指信息'),
    # StockIndex('000994', '全指通信'),
    # StockIndex('000986', '全指能源'),
    # StockIndex('000987', '全指材料'),
    # StockIndex('000988', '全指工业'),
    # StockIndex('000995', '全指公用'),
    # StockIndex('399997', '中证白酒'),
    # StockIndex('399987', '中证酒'),
    StockIndex('HS2083', '恒生科技'),
]

xData = []
yDataList = []
for index in symbolList:
    df = ak.index_zh_a_hist(symbol=index.code, start_date=startDate, end_date=endDate, period="weekly")
    if len(xData) == 0:
        xData = df['日期'].tolist()
    yDataList.append(df['收盘'])
    print(df)
    print(f'{index.name} 获取数据成功')


# 折线图
chart = Line(
    # 设置主题、画布宽度、高度等信息
    init_opts=opts.InitOpts(theme=ThemeType.DARK, width="1400px", height="600px"),
)

# 全局设置
chart.set_global_opts(
    title_opts=opts.TitleOpts(title="指数走势"),
    # 工具箱配置
    toolbox_opts=opts.ToolboxOpts(is_show=True),
    # 区域缩放配置
    datazoom_opts=[opts.DataZoomOpts(is_show=True, range_start=0, range_end=100)],
    # y轴配置
    yaxis_opts=opts.AxisOpts(
        interval=1000,  # 设置y轴刻度间隔为20
        # splitline_opts=OptSplitLine(is_show=True),  # 显示分割线
    )
)

chart.add_xaxis(xaxis_data=xData)  # 添加x轴数据
for i, d in enumerate(yDataList):
    chart.add_yaxis(
        series_name=symbolList[i].name,
        y_axis=d,  # y轴数据
        is_connect_nones=True,  # 连接空数据
        markpoint_opts=opts.MarkPointOpts(
            data=[opts.MarkPointItem(type_="max"), opts.MarkPointItem(type_="min")],
        ),
    )

chart.render(f"指数走势对比-{endDate}.html")

